package com.lanou.musicproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.RotateInfoActivity;
import com.lanou.musicproject.adapter.SongListAdapter;
import com.lanou.musicproject.bean.SongList;
import com.lanou.musicproject.tools.NoScrollGridView;

import java.util.List;

/**
 * Created by 王世美 on 15/10/21.
 * 歌单页面
 */
public class SongListFragment extends Fragment {
private NoScrollGridView songListGV;
    // 初始化组件
    private ViewPager viewlistPager;

    // 定义歌单实体类数组
    private List<SongList.ContentEntity> data;
    // 定义一个request请求队列
    private RequestQueue requestQueue;
    // 定义一个歌单适配器
    private SongListAdapter adapter;
    // 定义数据接口网址
    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.diy.gedan&page_size=30&page_no=1";
    public ViewPager getViewPager() {
        return viewlistPager;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_song_list,null);
        songListGV = (NoScrollGridView) v.findViewById(R.id.songListGV);
        songListGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SongList.ContentEntity entity = (
                        SongList.ContentEntity) parent.getItemAtPosition(position);
                Intent intent = new Intent(getActivity(),RotateInfoActivity.class);
                intent.putExtra("listid", entity.getListid());
                startActivity(intent);
            }
        });
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = Volley.newRequestQueue(getActivity());
        this.addData();
    }

    // 实体类解析方法
    public void addData() {
        String getUrl = path;
        StringRequest request = new StringRequest(getUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (response != null) {
                            Gson gson = new Gson();
                            SongList result = gson.fromJson(response, SongList.class);

                            if (result != null && (
                                    data = result.getContent()) != null) {
                                adapter = new SongListAdapter(getActivity(), data);
                                songListGV.setAdapter(adapter);
                            }
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }
}
